import os, random, shutil


def moveFile(fileDir, tarDir, rate=0.1):
        pathDir = os.listdir(fileDir)
        filenumber = len(pathDir)
        rate = rate
        picknumber = int(filenumber*rate)
        sample = random.sample(pathDir, picknumber)
        print(sample)
        for name in sample:
            shutil.move(os.path.join(fileDir, name), tarDir)
        return


def rename(path, add_name):
    for root,dirs,files in os.walk(path):
        for file in files:
            file_path = os.path.join(root, file)
            os.rename(file_path,os.path.join(root, add_name + file))


if __name__ == '__main__':
    m_fileDir = "/home/ailab/disk/disk_link/dataset/new_data/train"
    m_tarDir = '/home/ailab/disk/disk_link/dataset/new_data/val'
    for root, dirs, files in os.walk(m_fileDir):
        for dir in dirs:
            s_path = os.path.join(m_fileDir, dir)
            t_path = os.path.join(m_tarDir, dir)
            if not os.path.exists(t_path):
                os.makedirs(t_path)
            moveFile(s_path, t_path, 0.1)